package sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Tag(name = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Operation(summary = "新增菜品",description = "新增菜品")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功新增菜品")
    )
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        String key = "dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    @Operation(summary = "菜品分页查询",description = "菜品分页查询")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功查询菜品分页信息")
    )
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @Operation(summary ="菜品批量删除",description = "菜品批量删除" )
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功删除菜品")
    )
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids){
        //传入参数与方法参数名一致 可省略RequestParam的value属性
        log.info("批量删除菜品：{}",ids);
        dishService.deleteBatch(ids);
        // 清理所有菜品的缓存数据
        clickCache("dish_*");
        return Result.success();
    }

    @Operation(summary = "根据id查询菜品信息",description = "根据id查询菜品信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功查询菜品信息")
    )
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品信息:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @Operation(summary = "修改菜品信息",description = "修改菜品信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功修改菜品信息")
    )
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
    	log.info("修改菜品信息：{}",dishDTO);
    	dishService.updateWithFlavor(dishDTO);
        //清理所有菜品的缓存数据
        clickCache("dish_*");
        return Result.success();
    }

    @Operation(summary = "根据分类id查询菜品",description = "根据分类id查询菜品")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功查询菜品")
    )
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> list = dishService.getByCategoryId(categoryId);
        return Result.success(list);
    }
    @PostMapping("status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id){
        log.info("启用禁用菜品状态{}:{}",id,status==1?"启用":"禁用");
        //清理所有菜品的缓存数据
        clickCache("dish_*");
        dishService.setStatus(status,id);
        return Result.success(status);
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void clickCache(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null) {
            redisTemplate.delete(keys);
        }
    }
}
